/**
 * 页面初始化后，绑定函数。
 */
$(function(){
	//注册
	$("#registry").click(function(){
		register();
	});
	
	//登录
	$("#login").click(function(){
		login();
	});
	
	//登出
	$("#logout").click(function(){
		logout();
	});
	
	//修改密码
	$("#changePassword").click(function(){
		changepwd();
	})
	
});

//注册
function register() {

	// 取值
	var userName = $("#username").val();
	var password = $("#password").val();
	var password2 = $("#password2").val();
	var nickname = $("#nickname").val();
	// console.log(userName+password);

	// 校验用户名格式
	var reg = /^\w{3,20}$/;
	if(!reg.test(userName)) {
		$("#warning_1").text("3-20位的英文、数字、下划线.").show();
		return;
	}else{
		$("#warning_1").hide();
	}
	//校验密码
	if(password.length<6) {
		// alert("密码不一致或密码太短");
		$("#warning_2").text("密码太短小于6位").show();
		return;
	}
	else{
		$("#warning_2").hide();
	}
	if(password !== password2) {
		 $("#warning_3").text("密码不一致").show();
		 return ;
	 }else{
	 	$("#warning_3").hide();
	}
	//进行注册
	var user = {
		"cnUserName":userName,
		"cnUserPassword":password,
		"cnUserNick":nickname
	};
	// alert("dfssdfsd");
	$.ajax({
		type:"post",
		url:"cnUser/add.do",
		dataType:"json",
		data:user,
		success:function(result) {
			if(result.status==0) {
				if(result.data) {
					alert("注册成功.");
				} else {
					$("#warning_1").text("用户名已存在.").show();
				}
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

//登陆
function login() {
	var count = $("#username").val();
	var password = $("#password").val();
	if(count=="" || password=="") {
		$("#warning_1").text("用户名或密码不能为空.").show();
		return;
	}else{
		$("#warning_1").hide();
	}
	$.ajax({
		type:"post",
		url:"cnUser/login.do",
		dataType:"json",
		data:{"username":count,"password":password},
		success:function(result) {
			if(result.status==0) {
					//登陆成功，进入系统
				    //alert("登录成功.")
					window.location.href="edit.html";
					addCookie("userId",result.data.cnUserId,5);
					addCookie("userName",result.data.cnUserName,5);
				    alert(result.msg);
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/**
 * 退出登录
 */
function logout(){
	//注销登陆信息
	$.ajax({
		type:"post",
		url:"cnUser/logout.do",
		dataType:"json",
		data:{},
		success:function(result) {
			if(result.status==0) {
				//退回到登陆页
				location.href="login.html";
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/**
 * 修改密码
 */
function changepwd(){
	var password = $("#password").val();
	var newpassword = $("#new_password").val();
	var finalPassword = $("#final_password").val();
	var cookieuserName=getCookie('userName')
	if(newpassword.length < 6) {
		alert("密码长度不能小于6位.");
		return;
	} else if(newpassword != finalPassword) {
		alert("两次输入的新密码不一致.");
		return;
	}

	$.ajax({
		type:"post",
		url:"cnUser/change.do",
		dataType:"json",
		data:{
			"newpassword":newpassword,
			"finalPassword":finalPassword,
			"userName":cookieuserName
		},
		success:function(result) {
			if(result.status==0) {
				alert("密码修改成功");
				logout();
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		}
	}
	return ""
}
